from celery.worker.state import total_count

from file.models import File
from rest_framework import serializers
from core.response import StandardPagination


class FileSerializer(serializers.ModelSerializer): # 返回 datetime 对象
    class Meta:
        model = File
        fields = '__all__'

def get_files_by_knowbase_id(knowbase_id,request):
    queryset = File.objects.filter(know_base_id=knowbase_id).order_by('know_base_id')
    paginator = StandardPagination()
    paginated_queryset = paginator.paginate_queryset(queryset, request)
    serializer = FileSerializer(paginated_queryset, many=True)
    files_data = serializer.data
    return files_data,paginator


def get_file_by_file_id(file_id):
    files = File.objects.filter(file_id=file_id)
    result = []
    if len(files) == 0:
        return result
    serializer = FileSerializer(files, many=True)
    return serializer.data

def delete_files_by_knowbase_id(knowbase_id):
    delete_count,delete_details = File.objects.filter(know_base_id=knowbase_id).delete()
    return delete_count

def delete_file_by_file_id(file_id):
    delete_count,delete_details = File.objects.filter(file_id=file_id).delete()
    return delete_count


